package bbs

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bbs"
	"github.com/flipped-aurora/gin-vue-admin/server/model/bbs/request"
)

// 定义bbs的service提供BbsCategory的数据curd的操作

type BbsCategoryService struct{}

// @author: feige
// @function: CreateBbsCategory
// @description: 创建文字分类
// @param: e bbs.BbsCategory
// @return: err error
func (cbbs *BbsCategoryService) CreateBbsCategory(bbsCategory *bbs.BbsCategory) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.GVA_DB.Create(bbsCategory).Error
	return err
}

// @author: feige
// @function: UpdateBbsCategory
// @description: 更新文字分类
// @param: e *model.ExaCustomer
// @return: err error
func (cbbs *BbsCategoryService) UpdateBbsCategory(bbsCategory *bbs.BbsCategory) (err error) {
	err = global.GVA_DB.Model(bbsCategory).Updates(bbsCategory).Error
	return err
}

// @author: feige
// @function: DeleteBbsCategory
// @description: 删除帖子
// @param: e model.DeleteBbsCategory
// @return: err error
func (cbbs *BbsCategoryService) DeleteBbsCategory(bbsCategory *bbs.BbsCategory) (err error) {
	err = global.GVA_DB.Delete(&bbsCategory).Error
	return err
}

// @author: feige
// @function: DeleteBbsCategoryById
// @description: 根据ID删除帖子
// @param: e model.DeleteBbsCategoryById
// @return: err error
func (cbbs *BbsCategoryService) DeleteBbsCategoryById(id uint) (err error) {
	var BbsCategory bbs.BbsCategory
	err = global.GVA_DB.Where("id = ?", id).Delete(&BbsCategory).Error
	return err
}

// @author: feige
// @function: DeleteBbsCategoryById
// @description: 根据id批量删除
// @param: e model.DeleteBbsCategoryByIds
// @return: err error
func (cbbs *BbsCategoryService) DeleteBbsCategoryByIds(bbsCategorys []bbs.BbsCategory) (err error) {
	err = global.GVA_DB.Delete(&bbsCategorys).Error
	return err
}

// @author: feige
// @function: GetBbsCategory
// @description: 根据ID获取帖子信息
// @param: id uint
// @return: BbsCategory *bbs.BbsCategory, err error
func (cbbs *BbsCategoryService) GetBbsCategory(id uint) (bbsCategory *bbs.BbsCategory, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&bbsCategory).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomerInfoList
//@description: 分页获取客户列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (cbbs *BbsCategoryService) LoadBbsCategoryPage(info request.BbsCategoryPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.GVA_DB.Model(&bbs.BbsCategory{})

	// 准备切片帖子数组
	var BbsCategoryList []bbs.BbsCategory

	// 加条件
	if info.Keyword != "" {
		db = db.Where("title like ?", "%"+info.Keyword+"%")
	}

	if info.Status != -1 {
		db = db.Where("status = ?", info.Status)
	}
	// 排序默时间降序降序
	db = db.Order("sorted asc")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return BbsCategoryList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Find(&BbsCategoryList).Error
	}

	// 结果返回
	return BbsCategoryList, total, err
}
